package com.css.fxfzaqbz.modules.data.service.impl;

import com.css.fxfzaqbz.constants.YNEnum;
import com.css.fxfzaqbz.modules.data.entity.SchoolData;
import com.css.fxfzaqbz.modules.data.queryParams.SchoolDataQueryParams;
import com.css.fxfzaqbz.modules.data.repository.SchoolDataRepository;
import com.css.fxfzaqbz.modules.data.service.SchoolDataService;
import com.css.fxfzaqbz.util.FileUtil2;
import com.css.fxfzaqbz.util.PlatformDateUtils;
import com.css.fxfzaqbz.util.UUIDGenerator;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

@Service
public class SchoolDataServiceImpl implements SchoolDataService {
    @Resource
    SchoolDataRepository schoolDataRepository;

//    @Override
//    @Transactional(rollbackFor = Exception.class)
//    public SchoolData updateActiveFaultData(ActiveFaultData activeFaultData) {
//
//        activeFaultData.setAfdUpdatetime(PlatformDateUtils.getCurrentTimestamp());
//
//        return activeFaultDataRepository.updateActiveFaultData(activeFaultData);
//    }

    @Override
    @Transactional
    public SchoolData saveSchoolData(SchoolData schoolData) {
        schoolData.setSdId(UUIDGenerator.getUUID());
        schoolData.setDelFlag(YNEnum.Y.toString());
        schoolData.setCreateTime(PlatformDateUtils.getCurrentTimestamp());
        return schoolDataRepository.saveSchoolData(schoolData);
    }

    @Override
    public Map<String,Object> getAllSchoolData(SchoolDataQueryParams queryParams) {
        return schoolDataRepository.getAllSchoolDataParams(queryParams);
    }

    @Override
    public SchoolData getSchoolDataById(String id) {
        return schoolDataRepository.getSchoolDataById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteSchoolDatas(List<String> ids,String userId) {
        Timestamp timestamp = PlatformDateUtils.getCurrentTimestamp();
        schoolDataRepository.deleteSchoolDatasById(ids,timestamp,userId);
    }

    @Override
    public List<SchoolData> getAll() {

        return schoolDataRepository.getAll();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void importExcel(MultipartFile file) throws IOException {
        List<SchoolData> schoolData = FileUtil2.importExcel(file, SchoolData.class);
        for(int i = 0 ; i<schoolData.size();i++){
            schoolData.get(i).setSdId(UUIDGenerator.getUUID());
            schoolData.get(i).setDelFlag(YNEnum.Y.toString());
            schoolData.get(i).setCreateTime(PlatformDateUtils.getCurrentTimestamp());
        }
        schoolDataRepository.importExcel(schoolData);
    }
}
